<template>
  <div id="TopicChecked">
    <el-breadcrumb
      separator="/"
      style="padding-left: 10px; padding-bottom: 10px; font-size: 12px"
    >
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动审核</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card" shadow="hover">
      <el-form
        :inline="true"
        :model="queryMap"
        size="small"
        class="demo-form-inline"
      >
        <el-form-item label="活动名称">
          <el-input
            v-model="queryMap.topicName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="学生姓名">
          <el-input v-model="queryMap.stuName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="班级">
          <el-select
            v-model="queryMap.classId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入班级"
            :remote-method="getClassByKey"
            :loading="loading"
            clearable
          >
            <el-option
              v-for="item in classList"
              :key="item.classId"
              :label="item.className"
              :value="item.classId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动类型">
          <el-select
            v-model="queryMap.topicType"
            placeholder="请选择活动类型"
            clearable
          >
            <el-option
              v-for="item in topicTypes"
              :key="item.topicTypeId"
              :label="item.topicTypeName"
              :value="item.topicTypeId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select
            v-model="queryMap.type"
            placeholder="请选择审核状态"
            clearable
          >
            <el-option :key="0" label="审核不通过" :value="0"></el-option>
            <el-option :key="1" label="已提交" :value="1"></el-option>
            <el-option :key="2" label="管理员已审核" :value="2"></el-option>
            <el-option :key="3" label="教师已审核" :value="3"></el-option>
            <el-option :key="4" label="审核通过" :value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="search"
            type="primary"
            icon="el-icon-search"
            :loading="loading"
            round
            >查询</el-button
          >
          <el-button
            @click="checkTopicList"
            icon="el-icon-check"
            :loading="loading"
            :disabled="this.stuTopicListVO.selTopicIds.length === 0"
            round
            >批量审核</el-button
          >
        </el-form-item>
      </el-form>
      <template>
        <el-table
          :data="stuTopics"
          border
          stripe
          size="mini"
          style="width: 100%; margin-top: 5px"
          @selection-change="selsChange"
        >
          <el-table-column
            type="selection"
            width="55"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="stuNum"
            label="学号"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="stuName"
            label="姓名"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="className"
            label="班级"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="topicName"
            label="活动名称"
            align="center"
          ></el-table-column>
          <el-table-column prop="submitTime" label="提交时间" align="center">
            <template slot-scope="scope">{{
              scope.row.submitTime | dateFormat
            }}</template></el-table-column
          >
          <el-table-column
            prop="scoreRange"
            label="分数范围"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="score"
            label="上报分数"
            align="center"
          ></el-table-column>
          <el-table-column prop="type" label="审核状态" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.type == 0" style="color: #ff4949"
                >审核不通过</span
              >
              <span v-if="scope.row.type == 1">已提交</span>
              <span v-if="scope.row.type == 2" style="color: #e0f516"
                >管理员已审核</span
              >
              <span v-if="scope.row.type == 3" style="color: #409eff"
                >教师已审核</span
              >
              <span v-if="scope.row.type == 4" style="color: #13ce66"
                >审核通过</span
              >
            </template></el-table-column
          >
          <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
              <el-popconfirm
                confirmButtonText="审核"
                cancelButtonText="撤回"
                icon="el-icon-info"
                iconColor="red"
                title="确定您当前的操作吗？"
                @onConfirm="checkTopics(scope.row.stuId, scope.row.topicId)"
                @onCancel="
                  cancelCheckTopics(scope.row.stuId, scope.row.topicId)
                "
              >
                <el-button
                  slot="reference"
                  icon="el-icon-thumb"
                  type="text"
                  size="mini"
                  :disabled="
                    (scope.row.type == 4 && scope.row.score <= 5) ||
                    scope.row.type == 0
                  "
                  >审核</el-button
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <el-pagination
        style="margin-top: 10px"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryMap.page"
        :page-sizes="[10, 50, 100]"
        :page-size="this.queryMap.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <el-dialog title="撤回原因" :visible.sync="show" width="625px">
      <el-form :model="cancelReason" align="center">
        <el-form-item label="撤回原因" label-width="75px">
          <el-input
            v-model="cancelReason.reason"
            autocomplete="off"
            width="15px"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCancel" :loading="loading"
          >确 定</el-button
        >
        <el-button @click="show = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  queryStuTopicByPage,
  queryTopicTypes,
  queryClassesByClassName,
  checkedTopicState,
  cancelTopic,
  checkTopicList,
} from "api/api";
import { mapState } from "vuex";
export default {
  computed: {
    ...mapState(["userInfo"]),
  },
  data() {
    return {
      stuTopics: [],
      queryMap: { page: 1, size: 10 },
      total: 0,
      loading: false,
      show: false,
      topicTypes: [],
      classList: [],
      cancelReason: {},
      stuTopicListVO: {
        selTopicIds: [],
        selStuIds: [],
      },
    };
  },
  methods: {
    //批处理
    selsChange(sels) {
      this.stuTopicListVO.selTopicIds = [];
      this.stuTopicListVO.selStuIds = [];
      sels.forEach((sel) => {
        this.stuTopicListVO.selStuIds.push(sel.stuId);
        this.stuTopicListVO.selTopicIds.push(sel.topicId);
      });
    },
    //改变页码
    handleSizeChange(newSize) {
      this.queryMap.size = newSize;
      this.getStuTopicByPage();
    },
    //翻页
    handleCurrentChange(current) {
      this.queryMap.page = current;
      this.getStuTopicByPage();
    },
    //获取学生活动审核信息
    async getStuTopicByPage() {
      await queryStuTopicByPage(this.userInfo.sessionId, this.queryMap).then(
        (res) => {
          if (!res.flag) {
            this.$message.error(res.message);
          } else {
            this.total = res.data.total;
            this.stuTopics = res.data.rows;
          }
        }
      );
    },
    async search() {
      await queryStuTopicByPage(this.userInfo.sessionId, this.queryMap).then(
        (res) => {
          //初始化操作
          this.total = 0;
          this.stuTopics = [];
          if (!res.flag) {
            this.$message.error(res.message);
          } else {
            this.total = res.data.total;
            this.stuTopics = res.data.rows;
          }
        }
      );
    },
    //获取活动类型列表
    getTopicTypes() {
      queryTopicTypes(this.userInfo.sessionId).then((res) => {
        if (!res.flag) {
          this.$message.error(res.message);
        } else {
          this.topicTypes = res.data;
        }
      });
    },
    //获取班级信息
    async getClassByKey(query) {
      if (query != null) {
        queryClassesByClassName(this.userInfo.sessionId, query).then((res) => {
          if (!res.flag) {
            this.$message.error(res.message);
          } else {
            this.classList = res.data;
          }
        });
      }
    },
    //审核当前活动状态
    async checkTopics(stuId, topicId) {
      await checkedTopicState(this.userInfo.sessionId, stuId, topicId).then(
        (res) => {
          if (!res.flag) {
            this.$message.error(res.message);
          } else {
            this.$message({
              type: "success",
              message: "审核成功！",
            });
            this.getStuTopicByPage();
          }
        }
      );
    },
    cancelCheckTopics(stuId, topicId) {
      this.show = true;
      this.cancelReason.stuId = stuId;
      this.cancelReason.topicId = topicId;
    },
    //撤回活动
    async submitCancel() {
      await cancelTopic(this.userInfo.sessionId, this.cancelReason).then(
        (res) => {
          if (!res.flag) {
            this.$message.error(res.message);
          } else {
            this.$message({
              type: "success",
              message: "撤回成功！",
            });
            this.getStuTopicByPage();
            this.show = false;
          }
        }
      );
    },
    //批量审核
    async checkTopicList() {
      var res = await this.$confirm("此操作将批量审核活动，是否继续", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消操作",
        });
        return;
      });
      if (res == "confirm") {
        await checkTopicList(this.userInfo.sessionId, this.stuTopicListVO).then(
          (res) => {
            if (!res.flag) {
              this.$message.error(res.message);
            } else {
              this.$message({
                type: "success",
                message: "审核成功！",
              });
              this.getStuTopicByPage();
            }
          }
        );
      }
    },
  },
  mounted() {
    this.getStuTopicByPage();
    this.getTopicTypes();
  },
};
</script>